package org.dromara.travel.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.travel.domain.vo.EventsVo;
import org.dromara.travel.domain.bo.EventsBo;
import org.dromara.travel.service.IEventsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 活动
 *
 * @author Jacheut
 * @date 2024-04-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/travel/events")
public class EventsController extends BaseController {

    private final IEventsService eventsService;

    /**
     * 查询活动列表
     */
    @SaCheckPermission("travel:events:list")
    @GetMapping("/list")
    public TableDataInfo<EventsVo> list(EventsBo bo, PageQuery pageQuery) {
        return eventsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出活动列表
     */
    @SaCheckPermission("travel:events:export")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EventsBo bo, HttpServletResponse response) {
        List<EventsVo> list = eventsService.queryList(bo);
        ExcelUtil.exportExcel(list, "活动", EventsVo.class, response);
    }

    /**
     * 获取活动详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("travel:events:query")
    @GetMapping("/{id}")
    public R<EventsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(eventsService.queryById(id));
    }

    /**
     * 审批流程查看
     *
     * @param id 主键
     */
    @SaCheckPermission("travel:events:query")
    @GetMapping("/{id}/approve")
    public R<EventsVo> getApproveInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(eventsService.queryById(id));
    }

    /**
     * 新增活动
     */
    @SaCheckPermission("travel:events:add")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EventsBo bo) {
        return toAjax(eventsService.insertByBo(bo));
    }

    /**
     * 修改活动
     */
    @SaCheckPermission("travel:events:edit")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EventsBo bo) {
        return toAjax(eventsService.updateByBo(bo));
    }

    /**
     * 管理员审批活动
     */
    @SaCheckPermission("travel:events:edit")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/approve")
    public R<Void> approve(@RequestBody EventsBo bo) {
        return toAjax(eventsService.updateByBo(bo));
    }

    /**
     * 删除活动
     *
     * @param ids 主键串
     */
    @SaCheckPermission("travel:events:remove")
    @Log(title = "活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(eventsService.deleteWithValidByIds(List.of(ids), true));
    }
}
